const domain = "http://127.0.0.1:8088/";
axios.defaults.withCredentials = true

const permission = async function (menuCode) {
    let result = [];
    await axios.get(domain + "/roleAuth/getRolePermission", {
        headers: {
            "menuCode": menuCode
        }
    }).then(resp => {
        let data = resp.data;
        if (data.status == 200) {
            result = data.data;
        }
    });
    return result;
};

const menuButtonByOn = async function (menuCode) {
    let result = [];
    await axios.get(domain + "/roleAuth/getMenuPermission", {
        headers: {
            "menuCode": menuCode
        }
    }).then(resp => {
        let data = resp.data;
        if (data.status == 200) {
            result = data.data;
        }
    });
    return result;
}

const initAuth = async function (auth) {
    let roleAuth = await permission(auth.menuCode);
    let menuAuth = await menuButtonByOn(auth.menuCode);
    for (let key in auth) {
        if (key == 'menuCode' && typeof auth[key] == 'string') {
            continue;
        }
        if (roleAuth.includes(key) && menuAuth.includes(key)) {
            auth[key] = true;
        } else {
            auth[key] = false;
        }
    }
}

const notify = function(resp){
    let respStatus = resp.status;
    if(respStatus==200){
        let status = resp.data.status;
        let msg = resp.data.msg;
        if (status == 200){
            Vue.prototype.$message({
                offset:10,
                duration: 1500,
                message: msg,
                type: 'success'
            });
            return true;
        }else {
            Vue.prototype.$message({
                offset:10,
                duration: 1500,
                message: msg,
                type: 'warning'
            });
            return false;
        }
    }else{
        Vue.prototype.$message({
            offset:10,
            message: '好像出错了，请联系管理员！',
            type: 'error'
        });
        return false;
    }
}

const notifyAndReload = function (_this,resp,initTableFun){
    if(notify(resp)){
        eval("_this."+initTableFun +"()");
        return true;
    }
    return false;
}

const notifyAndReloadAndDialog = function (_this,resp,initTableFun,dialogVisible){
    if(notifyAndReload(_this,resp,initTableFun)){
        eval("_this."+dialogVisible +" = false");
        return true;
    }
    return false;
}


//获取页面之间传递的参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURI(r[2]);
    }
    return null;
}

Vue.filter('dateTime2DateAndTime',function (dateTime){
    const split = dateTime.split(" ");
    return "<div style='text-align: center'>"+ split[0] + "</div>" +
        "<div style='text-align: center'>" + split[1] +"</div>";
})
